package com.qf.service.impl;

import com.qf.entity.*;
import com.qf.mapper.ClassroomMapper;
import com.qf.mapper.StuMapper;
import com.qf.mapper.SubjectMapper;
import com.qf.service.StuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StuServiceImpl implements StuService {

    @Autowired
    private StuMapper stuMapper;

    @Autowired
    private ClassroomMapper classroomMapper;

    @Autowired
    private SubjectMapper Subjectmapper;

    @Override
    public Stu queryStuById(Integer id) {
        return stuMapper.queryStuById(id);
    }

    @Override
    public List<Stu> queryAll() {
        return stuMapper.queryAll();
    }

    @Override
    public List<Stu> queryLike(String sname, Integer age) {

        return stuMapper.queryLike(sname, age);
    }

    @Override
    public List<Stu> queryLikeMap(Map<String, Object> map) {
        return stuMapper.queryLikeMap(map);
    }

    @Override
    public int updateStu(Stu stu) {
        return stuMapper.updateStu(stu);
    }

    @Override
    public int addStu(Stu stu) {
        return stuMapper.addStu(stu);
    }

    @Override
    public int deleteStu(Integer id) {
        //判断id是否存在
        Stu stu = stuMapper.queryStuById(id);
        if (stu == null) {
            return 0;
        }
        return stuMapper.deleteStu(id);
    }

    @Override
    public int deleteBatch(List<Integer> ids) {
        return stuMapper.deleteBatch(ids);
    }

    @Override
    public List<StudentVO> queryAllStuAndClass() {
        return stuMapper.queryAllStuAndClass();
    }

    @Override
    public StudentVO queryStuAndSubject(Integer id) {
        return stuMapper.queryStuAndSubject(id);
    }

    @Override
    public StudentVO queryStuAndSubjectAndTeacher(Integer id) {
        return stuMapper.queryStuAndSubjectAndTeacher(id);
    }

    @Override
    public List<StudentVO> queryAllStuAndClassV2() {
        List<StudentVO> list = new ArrayList<>();
        List<Stu> studentList = stuMapper.queryAll();
//遍历得到所有的学生， 再根据学生对象中的教室id查询对应的教室
        for (Stu stu : studentList) {
            Classroom classroom = classroomMapper.queryClassroomById(stu.getCid());

            // 创建最终封装数据的对象
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(stu, studentVO);
            studentVO.setClassroom(classroom);
            list.add(studentVO);
        }
        return list;
    }


    @Override
    public StudentVO queryStuAndSubjectV2(Integer id) {

        // 1. 先根据学生id查出一个学生信息
        // 2. 再根据学生id去中间关联表中查出该学生对应的所有学科id
        // 3. 再根据每一个学科id查出每一个学科对象
        // 4. 然后组装到一起
        Stu stu = stuMapper.queryStuById(id);
        List<Subject> subjectList = new ArrayList<>( );
        List<Integer> subIdList = stuMapper.querySubIdsByStuId(id);
        for (Integer subId : subIdList) {
            // 再根据学科id获得每一个学科对象
            Subject subject = Subjectmapper.querySubjectById(subId);
            subjectList.add(subject);
        }
        //封装
        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(stu, studentVO);
        studentVO.setSubjectList(subjectList);
        return studentVO;
    }


}

